





package com.example.function.project.controller;

import com.example.common.support.*;
import com.example.function.project.model.one.ProspectusTemplate;
import com.example.function.project.model.pojo.ProspectusTemplatePojo;
import com.example.function.project.service.IProspectusTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



/**
 * Description: <project模块controller >. <br>
 * <p>
 * <基本的crud>
 * </p>
 * generate time:2021-7-30 16:24:12
 *
 * @author hanqf
 * @version V1.0
 */

@RestController
@Slf4j
@RequestMapping("/proj")
public class ProspectusTemplateController{


	@Resource(name = "prospectusTemplateService")
	protected IProspectusTemplateService prospectusTemplateService;



	/**
	 * 查询全部ProspectusTemplate.
	 *
	 * @return AjaxResponse
	 */
	@RequestMapping(value = "/prospectus/templates")
	@CP_OperateLog(value = "查询全部ProspectusTemplate", type = CP_GlobalNamingConstant.OPERATE_LIST)
	public AjaxResponse prospectusTemplates() {
		final List<ProspectusTemplate> prospectusTemplateList = prospectusTemplateService.findAll();
		return AjaxResponse.success(ProspectusTemplatePojo.wrapper(prospectusTemplateList));
	}


	/**
	 * 直接下载文件
	 * 返回文件流
	 */
	@RequestMapping(value = "/prospectus/download/{fileId}")
	@CP_OperateLog(value = "下载项目文件", type = CP_GlobalNamingConstant.OPERATE_DOWNLOAD)
	public void downloadFile(@PathVariable("fileId") Long fileId, HttpServletRequest request, HttpServletResponse response) throws IOException {
		prospectusTemplateService.downLoadFile(fileId, request, response);
	}


	/**
	 * <h2>删除项目计划书模板文件</h2>
	 *
	 * @author hanqf
	 */
	@PostMapping("/prospectus/remove/{fileId}")
	@CP_OperateLog(value = "删除项目计划书模板文件", type = CP_GlobalNamingConstant.OPERATE_DELETE)
	public AjaxResponse deleteFile(@PathVariable("fileId") Long fileId) {

		prospectusTemplateService.delete(fileId);

		final List<ProspectusTemplate> files = prospectusTemplateService.findAll();
		return AjaxResponse.success(ProspectusTemplatePojo.wrapper(files));
	}

	/**
	 * <h2>上传项目计划书文件</h2>
	 *
	 * @author hanqf
	 */
	@PostMapping("/prospectus/upload")
	@CP_OperateLog(value = "上传项目计划书文件", type = CP_GlobalNamingConstant.OPERATE_UPLOAD)
	public AjaxResponse addFile(MultipartFile[] multipartFiles) {

		if (multipartFiles == null || multipartFiles.length == 0) {
			throw new CustomException(CustomExceptionType.USER_INPUT_ERROR, "未上传任何文件");
		}

		prospectusTemplateService.uploadFiles(multipartFiles);

		final List<ProspectusTemplate> files = prospectusTemplateService.findAll();

		return AjaxResponse.success(ProspectusTemplatePojo.wrapper(files));
	}


}

